$(document).ready(function(){
    // render
    loadData();
    // event
    bindEvent();
});

function loadData() {
    ISS.BACKEND.request({
        url: '../../service/admin/getOrderCommon',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapOrderToView(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: '../../service/admin/getOrderDetailCommon',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapOrderDetailToView(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: '../../service/admin/getAllMember',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapMemberToView(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: '../../service/admin/getAllOrderCode',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapOrderCodeToView(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: '../../service/admin/getAllProductDetailCode',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapProductDetailCodeToView(responseContent);
        }
    });
}

function bindEvent() {
    ISS.EVENT.bindEvent('#OrderCommon_Add', 'click', function () {
        if(validate()){
            sendInfo();
        }
        else {
            var htmlText = '<div class="box s-error mbs msgBox l-pageWrapper mbm">\
                                <div class="pam">Thêm đơn đặt hàng mới thất bại</div>\
                            </div>';
            ISS.DOM.before('.l-register__benefits', htmlText);
        }
    });
}

function sendInfo() {
    var order = {};
    var member = {};

    order.maDH = ISS.DOM.getVal('#OrderCommon_Code');
    member.maTV  = ISS.DOM.getVal('#OrderCommon_Member');
    order.thongTinThanhVien = member;
    order.hoTenNN = ISS.DOM.getVal('#OrderCommon_Receiver');
    order.soDTNN = ISS.DOM.getVal('#OrderCommon_ReceiverPhone');
    order.diaChiNN = ISS.DOM.getVal('#OrderCommon_ReceiverAddress');
    order.ngayDat = ISS.DOM.getVal('#OrderCommon_DateBegin');
    order.ngayGiao = ISS.DOM.getVal('#OrderCommon_DateEnd');
    order.hoanTat = ISS.DOM.getVal('#OrderCommon_Finish');
    order.ghiChu = ISS.DOM.getVal('#OrderCommon_Note');
    order.tongTien = ISS.DOM.getVal('#OrderCommon_Total');

    ISS.BACKEND.request({
        url: '../../service/admin/header/adOrderCommon',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(order),
        success: function (data) {
            var responseContent = data.responseObject;
        }
    });
}

function clearValidate() {
    ISS.DOM.removeClass('#OrderCommon_Code', 'error');
    ISS.DOM.removeClass('#OrderCommon_ReceiverPhone', 'error');
}

function validate() {
    var result = true;
    return result;
}

function mapOrderToView(order) {
    var rows = order.length;
    var columns = 1;
    var str = '';
    str += '<table class="scroll align-center">';
    str += '<thead>';
    str += '   <tr>';
    str += '       <th class="columnSTT">STT</th>';
    str += '       <th class="columnStick">Tick</th>';
    str += '       <th class="columnRule">Mã đơn</th>';
    str += '       <th class="columnRule">Người mua</th>';
    str += '       <th class="columnRule">Người nhận (NN)</th>';
    str += '       <th class="columnRule">Số điện thoại (NN)</th>';
    str += '       <th class="columnRule">Địa chỉ (NN)</th>';
    str += '       <th class="columnRule">Ngày đặt</th>';
    str += '       <th class="columnRule">Ngày giao</th>';
    str += '       <th class="columnRule">Trạng thái</th>';
    str += '       <th class="columnRule">Ghi chú</th>';
    str += '       <th class="columnRule">Tổng tiền</th>';
    str += '   </tr>';
    str += '</thead>';
    str += '<tbody>';
    for(var i = 0; i < rows; i++){
        if (i%2==0){
            str += '<tr class="column-Dark">';
        }else{
            str += '<tr class="column-Light">';
        }

        if (order[i].hoanTat==1){
            var result = 'Hoàn Tất';
        }else{
            result = 'Chưa hoàn tất';
        }

        for (var j = 0; j < columns; j++){
            str += '<td class="columnSTT">' + i + '</td>';
            str += '<td class="columnStick"><input id="' + order[i].maDH + '" type="checkbox" value="' + order[i].maDH + '"/></td>';
            str += '<td class="columnRule">' + order[i].maDH + '</td>';
            str += '<td class="columnRule" id="' + order[i].thongTinThanhVien.maTV + '">' + order[i].thongTinThanhVien.ho +
            order[i].thongTinThanhVien.ten + '</td>';
            str += '<td class="columnRule">' + order[i].hoTenNN + '</td>';
            str += '<td class="columnRule">' + order[i].soDTNN + '</td>';
            str += '<td class="columnRule">' + order[i].diaChiNN + '</td>';
            str += '<td class="columnRule">' + order[i].ngayDat + '</td>';
            str += '<td class="columnRule">' + order[i].ngayGiao + '</td>';
            str += '<td class="columnRule">' + result + '</td>';
            str += '<td class="columnRule">' + order[i].ghiChu + '</td>';
            str += '<td class="columnRule">' + order[i].tongTien + '</td>';
        }
        str += '</tr>';
    }
    str +=   '</tbody>';
    str += '</table>';
    ISS.DOM.html('#OrderCommon_All', str);
}

function mapOrderDetailToView(order) {
    var rows = order.length;
    var columns = 1;
    var str = '';
    str += '<table class="scroll align-center">';
    str += '<thead>';
    str += '   <tr>';
    str += '       <th class="columnSTT">STT</th>';
    str += '       <th class="columnStick">Tick</th>';
    str += '       <th class="columnRule">Mã CTĐH</th>';
    str += '       <th class="columnRule">Mã ĐH</th>';
    str += '       <th class="columnRule">Tên sản phẩm</th>';
    str += '       <th class="columnRule">Số lượng</th>';
    str += '       <th class="columnRule">Tổng giá (Số lượng x Đơn giá)</th>';
    str += '   </tr>';
    str += '</thead>';
    str += '<tbody>';
    for(var i = 0; i < rows; i++){
        if (i%2==0){
            str += '<tr class="column-Dark">';
        }else{
            str += '<tr class="column-Light">';
        }

        for (var j = 0; j < columns; j++){
            str += '<td class="columnSTT">' + i + '</td>';
            str += '<td class="columnStick"><input id="' + order[i].maDH + '" type="checkbox" value="' + order[i].maDH + '"/></td>';
            str += '<td class="columnRule">' + order[i].maDH + '</td>';
            str += '<td class="columnRule">' + order[i].datHang.maDH + '</td>';
            str += '<td class="columnRule">' + order[i].chiTietSanPham.maCTSP + ' - ' + order[i].chiTietSanPham.sanPham.tenSP + '</td>';
            str += '<td class="columnRule">' + order[i].soLuong + '</td>';
            str += '<td class="columnRule">' + order[i].tongGia + '</td>';
        }
        str += '</tr>';
    }
    str +=   '</tbody>';
    str += '</table>';
    ISS.DOM.html('#OrderDetailCommon_All', str);
}

function mapMemberToView(member) {
    var html = '<option selected="selected" value="popularity">Mời chọn...</option>';
    for(var i=0; i<member.length; i++) {
        html += '<option value="' + member[i].maTV + '">' + member[i].maTV + ' - ' + member[i].ho + ' ' + member[i].ten + '</option>';
    }
    ISS.DOM.html('#OrderCommon_Member', html);
}

function mapOrderCodeToView(ordercode) {
    var html = '<option selected="selected" value="popularity">Mời chọn...</option>';
    for(var i=0; i<ordercode.length; i++) {
        html += '<option value="' + ordercode[i].maDH + '">' + ordercode[i].maDH + ' - ' + ordercode[i].thongTinThanhVien.maTV + '</option>';
    }
    ISS.DOM.html('#OrderDetail_Option', html);
}

function mapProductDetailCodeToView(product) {
    var rows = product.length;
    var columns = 1;
    var str = '';
    str += '<table class="scroll align-center">';
    str += '<thead>';
    str += '   <tr>';
    str += '       <th class="columnSTT">STT</th>';
    str += '       <th class="columnStick">Tick</th>';
    str += '       <th class="columnList">Danh mục</th>';
    str += '       <th class="columnTitle-summary">Tên</th>';
    str += '       <th class="columnColor">Màu</th>';
    str += '       <th class="columnSize">Size</th>';
    str += '       <th class="columnSexual">ĐT</th>';
    str += '       <th class="columnPicture">Hình ảnh</th>';
    str += '       <th class="columnPatern">Hoạt tiết</th>';
    str += '       <th class="columnQuantity-Product-Detail-summary">SL hiện có</th>';
    str += '       <th class="columnQuantity-Product-Detail-summary">SL bán</th>';
    str += '       <th class="columnPrice-summary-product-detail">Đơn giá (VND)</th>';
    str += '       <th class="columnPrice-summary-product-detail">Tổng giá (SL*ĐG)</th>';
    str += '   </tr>';
    str += '</thead>';
    str += '<tbody>';
    for(var i = 0; i < rows; i++){
        if (i%2==0)
        {
            str += '<tr class="column-Dark">'
        }
        else
        {
            str += '<tr class="column-Light">'
        }
        for (var j = 0; j < columns; j++){
            str += '<td class="columnSTT">' + (i+1) + '</td>';
            str += '<td class="columnStick"><input id="' + product[i].maCTSP + '" type="checkbox" value="' + product[i].maCTSP + '"/></td>';
            str += '<td class="columnList" id="' + product[i].sanPham.loaiDanhMucSanPham.maLoaiDMSP + '">' + product[i].sanPham.loaiDanhMucSanPham.tenLoaiDMSP + '</td>';
            str += '<td class="columnTitle-summary">' + product[i].sanPham.tenSP + '</td>';
            str += '<td class="columnColor" id="' + product[i].mauSac.maMau + '">' + product[i].mauSac.tenMau + '</td>';
            str += '<td class="columnSize" id="' + product[i].kichThuoc.maSize + '">' + product[i].kichThuoc.tenSize + '</td>';
            str += '<td class="columnSexual">' + product[i].sanPham.doiTuong + '</td>';
            str += '<td class="columnPicture">Picture ' +  i + '.jpg</td>';
            str += '<td class="columnPatern" id="' + product[i].hoaTiet.maHoaTiet + '">' +  product[i].hoaTiet.tenHoaTiet + '</td>';
            str += '<td class="columnQuantity-Product-Detail-summary">' +   product[i].soLuong + '</td>';
            str += '<td class="columnQuantity-Product-Detail-summary"><input id="OrderDetail_QuantitySell_' + product[i].maCTSP + '" type="text"/></td>';
            str += '<td class="columnPrice-summary-product-detail">'+ product[i].giaSP +'</td>';
            str += '<td class="columnPrice-summary-product-detail"><input id="OrderDetail_TotalPrice_' + product[i].maCTSP + '" type="text"/></td>';
        }
        str += '</tr>';
    }
    str +=   '</tbody>';
    str += '</table>';
    ISS.DOM.html('#OrderDetail_Product', str);
}